package com.aktk.battlenole;

import com.aktk.gameelements.GamePlay.Orientation;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

//import com.aktk.battlenole.BoardCellState.Orientation;

public class BoardCell implements View.OnClickListener {
	private BoardCellState currentState;
	private ImageView control;
	private int row, column;
	public OnClickListener listener;
	
	public BoardCell (int row, int column)
	{
		this.row = row;
		this.column = column;
		currentState = new BoardCellState();
	}
	public void Draw (LinearLayout host)
	{
    	LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f);
		control = new ImageView(host.getContext());
		control.setAdjustViewBounds(true);
		control.setLayoutParams(params);
//		control.setMaxWidth(30); // Replace with method for scaling to screen resolution
//		control.setMaxHeight(30); // Replace with preserving aspect ratio
		control.setPadding(1, 0, 0, 0);
		control.setOnClickListener(this);
		host.addView(control);
		Refresh();
	}
	public void Refresh()
	{
		switch (currentState.getState())
		{
			case HIT:  
			{
				control.setImageResource(R.drawable.grid_center_shot);
				return;
			}
			case MISS: 
			{
				control.setImageResource(R.drawable.grid_water_shot);
				return;
			}
			  		   
			case NULL: 
			{
				control.setImageResource(R.drawable.grid_water);
				return;
			}
	  		default: 
	  		{
	  			break;
	  		}
		}
		if (currentState.getShot())
		{
			switch (currentState.getState())
			{
				case BOW:
				{
					if (currentState.getOrientation() == Orientation.HORIZONTAL)
					{
						control.setImageResource(R.drawable.grid_bow_horz_shot);
					}	
					else 
					{
						control.setImageResource(R.drawable.grid_bow_vert_shot);
					}
					break;
				}
				case STERN:
				{
					if (currentState.getOrientation() == Orientation.HORIZONTAL)
					{
						control.setImageResource(R.drawable.grid_stern_horz_shot);
					}	
					else 
					{
						control.setImageResource(R.drawable.grid_stern_vert_shot);
					}
					break;
				}
				case MID:
				{
					control.setImageResource(R.drawable.grid_center_shot);
					break;
				}
				case WHOLE:
				{
					control.setImageResource(R.drawable.grid_single);
					break;
				}
				default:
				{
					control.setImageResource(R.drawable.grid_water_shot);
					break;
				}
			}
		}
		else
		{
			switch (currentState.getState())
			{
				case BOW:
				{
					if (currentState.getOrientation() == Orientation.HORIZONTAL)
					{
						control.setImageResource(R.drawable.grid_bow_horz);
					}
					else 
					{
						control.setImageResource(R.drawable.grid_bow_vert);
					}	
					break;
				}
				case STERN:
				{
					if (currentState.getOrientation() == Orientation.HORIZONTAL)
					{
						control.setImageResource(R.drawable.grid_stern_horz);
					}
					else 
					{
						control.setImageResource(R.drawable.grid_stern_vert);
					}
					break;
				}
				case MID:
				{
					control.setImageResource(R.drawable.grid_center);
					break;
				}
				case WHOLE:
				{
					control.setImageResource(R.drawable.grid_single);
					break;
				}
				default:
				{
					control.setImageResource(R.drawable.grid_water);
					break;
				}
			}
		}
	}
	public int getRow () { return this.row;}
	public int getColumn () { return this.column; }
	
	public void setOnClickListener (OnClickListener listener){
		this.listener = listener;
	}
	public BoardCellState getCurrentState()
	{
		return currentState;
	}
	public void setCurrentState(BoardCellState state)
	{
		currentState = state;
		Refresh();
	}
	@Override
	public void onClick(View arg0) {
		if (listener != null)
		{
			listener.onClick(this);
		}
	}
	public static interface OnClickListener
	{
		public void onClick (BoardCell cell);
	}
}
